#coding:utf-8
from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render
from models import dircache_model, filecache_model
from django.contrib.auth.decorators import login_required
from forms import dirForm, fileForm
from Nodecontrol.models import node_info
from Nodecontrol.models import nodegroup
from urllib import urlopen
#import pycurl
import commands

@login_required(login_url="/login/")
def file_view(request):
    if request.method == 'POST':# 当提交表单时
        form_info = fileForm(request.POST) # form 包含提交的数据
        #print form_info
        #print request.POST
        if form_info.is_valid():# 如果提交的数据合法
            form_info.save()
            #iplist = []
            #iplist.append(request.POST.get('iplist'))
            nodeg = request.POST.get('nodegroup')
            print nodeg
            node_list = node_info.objects.filter(nodegroup=nodeg)
            print node_list
            url = request.POST.get('task_url')
            resOutput = []
            for node in  node_list:
                cmd =  'curl -X PURGE -x %s:5000 -v  "%s"'%(node.NodeIP,url)
                #s,o = commands.getstatusoutput(cmd)
                print  'curl -X PURGE -x %s:80 -v  "%s"'%(node.NodeIP,url)

                #异常处理
                #请求结果入库
            resStatus = 0
            resOutput = ['ok','ok']
            if resStatus:
                return render(request, 'purgeres.html',locals())#搞个table 刷？
           #    print  'curl -X PURGE -x %s:80 -v  "%s"'%(ip,url)
            return HttpResponseRedirect('/filecache')

    else:# 当正常访问时
        form_info = fileForm()
    group_list = nodegroup.objects.all()
    #cache_list = filecache_model.objects.all()
    return render(request, 'filecache.html',locals())


@login_required(login_url="/login/")
def dir_view(request):
    if request.method == 'POST':# 当提交表单时
        form_info = dirForm(request.POST) # form 包含提交的数据
        #print form_info
        #print request.POST
        #print request.POST["iplist"] 奇怪的取值 如果querydict包含list
        if form_info.is_valid():# 如果提交的数据合法
            form_info.save()
            nodeg = request.POST.get('nodegroup')
            print nodeg
            node_list = node_info.objects.filter(nodegroup=nodeg)
            print node_list
            url = request.POST.get('task_url')
            resOutput = []
            for node in  node_list:
                cmd =  'curl -X PURGE -x %s:5000 -v  "%s"'%(node.NodeIP,url)
                #s,o = commands.getstatusoutput(cmd)
                print  'curl -X PURGE -x %s:80 -v  "%s"'%(node.NodeIP,url)
            resStatus = 0
            resOutput = ['ok','ok']
            if resStatus:
                return render(request, 'purgeres.html',locals())#搞个table 刷？
            return HttpResponseRedirect('/dircache')


    else:# 当正常访问时
        form_info = dirForm()
    group_list = nodegroup.objects.all()
    #cache_list = filecache_model.objects.all()
    return render(request, 'dircache.html',locals())